{"ast":null,"code":"import { makeAutoObservable } from \"mobx\";\nimport { UserService, AuthService, ChatService } from \"../services/\";\nimport axios from \"axios\";\nimport { refreshToken } from \"../routes\";\nexport default class Store {\n  constructor() {\n    this.isAuth = false;\n    this.profile = {};\n    this.chats = {};\n    this.currentDialog = {};\n    this.viewedDialogId = null;\n    this.isLoading = false;\n    this.isError = false;\n    this.getAuth = () => {\n      return this.isAuth;\n    };\n    this.getLoading = () => {\n      return this.isLoading;\n    };\n    makeAutoObservable(this);\n  }\n  setAuth(bool) {\n    this.isAuth = bool;\n  }\n  getProfile() {\n    return this.profile;\n  }\n  setProfile(profile) {\n    this.profile = profile;\n  }\n  getChats() {\n    return this.chats;\n  }\n  setChats(chats) {\n    this.chats = chats;\n  }\n  getCurrentDialog() {\n    return this.currentDialog;\n  }\n  setCurrentDialog(currentDialog) {\n    this.currentDialog = currentDialog;\n  }\n  getViewedDialogId() {\n    return this.viewedDialogId;\n  }\n  setViewedDialogId(viewedDialogId) {\n    this.viewedDialogId = viewedDialogId;\n  }\n  setLoading(bool) {\n    this.isLoading = bool;\n  }\n  setIsError(bool) {\n    this.isError = bool;\n  }\n  getIsError() {\n    return this.isError;\n  }\n  setTokensTolLocalStorage(response) {\n    localStorage.setItem(`${process.env.REACT_APP_NAME_TOKEN}`, response.data.token);\n    localStorage.setItem(`${process.env.REACT_APP_NAME_REFRESH_TOKEN}`, response.data.refresh_token);\n  }\n  removeTokensTolLocalStorage() {\n    localStorage.removeItem(`${process.env.REACT_APP_NAME_TOKEN}`);\n    localStorage.removeItem(`${process.env.REACT_APP_NAME_REFRESH_TOKEN}`);\n  }\n  async getProfileFromAPI() {\n    try {\n      const response = await UserService.profile();\n      this.setProfile(response.data);\n    } catch (e) {\n      this.removeTokensTolLocalStorage();\n      this.setIsError(true);\n    }\n  }\n  async signIn(request) {\n    try {\n      const response = await AuthService.signIn(request);\n      this.setTokensTolLocalStorage(response);\n      this.setAuth(true);\n      await this.getProfileFromAPI();\n    } catch (e) {\n      this.setIsError(true);\n    }\n  }\n  async signUp(request) {\n    try {\n      const response = await AuthService.signUp(request);\n      this.setTokensTolLocalStorage(response);\n      this.setAuth(true);\n      await this.getProfileFromAPI();\n    } catch (e) {\n      this.setIsError(true);\n    }\n  }\n  async forgotPassword(request) {\n    try {\n      await AuthService.forgotPassword(request);\n    } catch (e) {\n      this.setIsError(true);\n    }\n  }\n  logout() {\n    this.removeTokensTolLocalStorage();\n    this.setAuth(false);\n    this.setProfile({});\n  }\n  async checkAuth() {\n    this.setLoading(true);\n    try {\n      const response = await axios.post(refreshToken(), {\n        \"refresh_token\": localStorage.getItem(`${process.env.REACT_APP_NAME_REFRESH_TOKEN}`)\n      });\n      this.setTokensTolLocalStorage(response);\n      this.setAuth(true);\n      await this.getProfileFromAPI();\n    } catch (e) {\n      this.removeTokensTolLocalStorage();\n      this.setIsError(true);\n    } finally {\n      this.setLoading(false);\n    }\n  }\n  async getChatsFromAPI() {\n    try {\n      return await ChatService.getChatsList().then(res => this.setChats(res.data));\n    } catch (e) {\n      var _e$response, _e$response$data;\n      if ((_e$response = e.response) !== null && _e$response !== void 0 && (_e$response$data = _e$response.data) !== null && _e$response$data !== void 0 && _e$response$data.message) {\n        console.error(e.response.data.message);\n      }\n    }\n  }\n  async deleteProfileFromAPI() {\n    try {\n      return await UserService.deleteProfile().then(res => {\n        console.log(res.data, res.data.refresh_token);\n        if (res.data.refresh_token) {\n          this.logout();\n        }\n      });\n    } catch (e) {\n      var _e$response2, _e$response2$data;\n      if ((_e$response2 = e.response) !== null && _e$response2 !== void 0 && (_e$response2$data = _e$response2.data) !== null && _e$response2$data !== void 0 && _e$response2$data.message) {\n        console.error(e.response.data.message);\n      }\n    }\n  }\n}","map":{"version":3,"names":["makeAutoObservable","UserService","AuthService","ChatService","axios","refreshToken","Store","constructor","isAuth","profile","chats","currentDialog","viewedDialogId","isLoading","isError","getAuth","getLoading","setAuth","bool","getProfile","setProfile","getChats","setChats","getCurrentDialog","setCurrentDialog","getViewedDialogId","setViewedDialogId","setLoading","setIsError","getIsError","setTokensTolLocalStorage","response","localStorage","setItem","process","env","REACT_APP_NAME_TOKEN","data","token","REACT_APP_NAME_REFRESH_TOKEN","refresh_token","removeTokensTolLocalStorage","removeItem","getProfileFromAPI","e","signIn","request","signUp","forgotPassword","logout","checkAuth","post","getItem","getChatsFromAPI","getChatsList","then","res","message","console","error","deleteProfileFromAPI","deleteProfile","log"],"sources":["/data/messenger.client/src/store/store.ts"],"sourcesContent":["import {makeAutoObservable} from \"mobx\";\nimport SignInRequest from \"../models/request/SignInRequest\";\nimport SignUpRequest from \"../models/request/SignUpRequest\";\nimport {UserService, AuthService, ChatService} from \"../services/\";\nimport {UserListItem} from \"../models/response/UserListItem\";\nimport axios, {AxiosResponse} from \"axios\";\nimport {AuthResponse} from \"../models/response/AuthResponse\";\nimport {refreshToken} from \"../routes\";\nimport ForgotPasswordRequest from \"../models/request/ForgotPasswordRequest\";\nimport {ChatsListResponse} from \"../models/response/ChatsListResponse\";\nimport {ChatsListItem} from \"../models/response/ChatsListItem\";\n\nexport default class Store {\n    private isAuth: boolean = false;\n    private profile = {} as UserListItem;\n    public chats = {} as ChatsListResponse;\n    public currentDialog = {} as ChatsListItem;\n    public viewedDialogId: number | null = null;\n    private isLoading: boolean = false;\n    private isError: boolean = false;\n\n    public constructor() {\n        makeAutoObservable(this);\n    }\n\n    private setAuth(bool: boolean): void {\n        this.isAuth = bool;\n    }\n\n    public getAuth: () => boolean = () => {\n        return this.isAuth;\n    }\n\n    public getProfile(): UserListItem {\n        return this.profile;\n    }\n\n    private setProfile(profile: UserListItem): void {\n        this.profile = profile;\n    }\n\n    public getChats(): ChatsListResponse {\n        return this.chats;\n    }\n\n    private setChats(chats: ChatsListResponse): void {\n        this.chats = chats;\n    }\n\n    public getCurrentDialog(): ChatsListItem {\n        return this.currentDialog;\n    }\n\n    public setCurrentDialog(currentDialog: ChatsListItem): void {\n        this.currentDialog = currentDialog;\n    }\n\n    public getViewedDialogId(): number | null {\n        return this.viewedDialogId;\n    }\n\n    public setViewedDialogId(viewedDialogId: number | null): void {\n        this.viewedDialogId = viewedDialogId;\n    }\n\n    private setLoading(bool: boolean): void {\n        this.isLoading = bool;\n    }\n\n    public getLoading: () => boolean = () => {\n        return this.isLoading;\n    }\n\n    public setIsError(bool: boolean): void {\n        this.isError = bool;\n    }\n\n    public getIsError(): boolean {\n        return this.isError;\n    }\n\n    public setTokensTolLocalStorage(response: AxiosResponse<AuthResponse>): void {\n        localStorage.setItem(\n            `${process.env.REACT_APP_NAME_TOKEN}`,\n            response.data.token\n        );\n        localStorage.setItem(\n            `${process.env.REACT_APP_NAME_REFRESH_TOKEN}`,\n            response.data.refresh_token\n        );\n    }\n\n    public removeTokensTolLocalStorage(): void {\n        localStorage.removeItem(`${process.env.REACT_APP_NAME_TOKEN}`);\n        localStorage.removeItem(`${process.env.REACT_APP_NAME_REFRESH_TOKEN}`);\n    }\n\n    private async getProfileFromAPI() {\n        try {\n            const response = await UserService.profile();\n            this.setProfile(response.data);\n        } catch (e) {\n            this.removeTokensTolLocalStorage();\n            this.setIsError(true);\n        }\n    }\n\n    public async signIn(request: SignInRequest) {\n        try {\n            const response = await AuthService.signIn(request);\n            this.setTokensTolLocalStorage(response);\n            this.setAuth(true);\n            await this.getProfileFromAPI();\n        } catch (e) {\n            this.setIsError(true);\n        }\n    }\n\n    public async signUp(request: SignUpRequest) {\n        try {\n            const response = await AuthService.signUp(request);\n            this.setTokensTolLocalStorage(response);\n            this.setAuth(true);\n            await this.getProfileFromAPI();\n        } catch (e) {\n            this.setIsError(true);\n        }\n    }\n\n    public async forgotPassword(request: ForgotPasswordRequest) {\n        try {\n            await AuthService.forgotPassword(request);\n        } catch (e) {\n            this.setIsError(true);\n        }\n    }\n\n    public logout() {\n        this.removeTokensTolLocalStorage();\n        this.setAuth(false);\n        this.setProfile({} as UserListItem);\n    }\n\n    public async checkAuth() {\n        this.setLoading(true);\n        try {\n            const response = await axios.post<AuthResponse>(\n                refreshToken(),\n                {\n                    \"refresh_token\": localStorage.getItem(`${process.env.REACT_APP_NAME_REFRESH_TOKEN}`)\n                }\n            );\n            this.setTokensTolLocalStorage(response);\n            this.setAuth(true);\n            await this.getProfileFromAPI();\n        } catch (e) {\n            this.removeTokensTolLocalStorage();\n            this.setIsError(true);\n        } finally {\n            this.setLoading(false);\n        }\n    }\n\n    public async getChatsFromAPI() {\n        try {\n            return await ChatService.getChatsList().then((res: any) => (\n                this.setChats(res.data)\n            ));\n        } catch (e: any) {\n            if (e.response?.data?.message!) {\n                console.error(e.response.data.message);\n            }\n        }\n    }\n\n    public async deleteProfileFromAPI() {\n        try {\n            return await UserService.deleteProfile().then((res: any) => {\n                console.log(res.data, res.data.refresh_token);\n\n                if (res.data.refresh_token) {\n                    this.logout();\n                }\n            });\n        } catch (e: any) {\n            if (e.response?.data?.message!) {\n                console.error(e.response.data.message);\n            }\n        }\n    }\n}"],"mappings":"AAAA,SAAQA,kBAAkB,QAAO,MAAM;AAGvC,SAAQC,WAAW,EAAEC,WAAW,EAAEC,WAAW,QAAO,cAAc;AAElE,OAAOC,KAAK,MAAuB,OAAO;AAE1C,SAAQC,YAAY,QAAO,WAAW;AAKtC,eAAe,MAAMC,KAAK,CAAC;EAShBC,WAAW,GAAG;IAAA,KARbC,MAAM,GAAY,KAAK;IAAA,KACvBC,OAAO,GAAG,CAAC,CAAC;IAAA,KACbC,KAAK,GAAG,CAAC,CAAC;IAAA,KACVC,aAAa,GAAG,CAAC,CAAC;IAAA,KAClBC,cAAc,GAAkB,IAAI;IAAA,KACnCC,SAAS,GAAY,KAAK;IAAA,KAC1BC,OAAO,GAAY,KAAK;IAAA,KAUzBC,OAAO,GAAkB,MAAM;MAClC,OAAO,IAAI,CAACP,MAAM;IACtB,CAAC;IAAA,KAsCMQ,UAAU,GAAkB,MAAM;MACrC,OAAO,IAAI,CAACH,SAAS;IACzB,CAAC;IAjDGb,kBAAkB,CAAC,IAAI,CAAC;EAC5B;EAEQiB,OAAO,CAACC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